home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / inetutil.1 / inetutil / inetutils-1.1 / talkd / announce.c next >
Encoding:
C/C++ Source or Header  |  1996-07-22  |  5.9 KB  |  204 lines

  1. /*
  2.  * Copyright (c) 1983, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)announce.c    8.3 (Berkeley) 4/28/95";
  36. #endif /* not lint */
  37.  
  38. #ifdef HAVE_CONFIG_H
  39. #include <config.h>
  40. #endif
  41.  
  42. #include <sys/types.h>
  43. #include <sys/uio.h>
  44. #include <sys/stat.h>
  45. #include <sys/time.h>
  46. #include <sys/wait.h>
  47. #include <sys/socket.h>
  48. #ifndef HAVE_OSOCKADDR
  49. #include <osockaddr.h>
  50. #endif
  51. #include <protocols/talkd.h>
  52. #include <sgtty.h>
  53. #include <errno.h>
  54. #include <syslog.h>
  55. #include <unistd.h>
  56. #include <stdio.h>
  57. #include <string.h>
  58. #ifdef HAVE_VIS_H
  59. #include <vis.h>
  60. #endif
  61. #include <paths.h>
  62.  
  63. extern char *hostname;
  64.  
  65. extern char *ttymsg ();
  66.  
  67. /*
  68.  * Announce an invitation to talk.
  69.  */
  70.     
  71. #ifndef HAVE_VIS_H
  72.  
  73. #define VIS_CSTYLE 0        /* dummy value */
  74.  
  75. /* A simpler version of bsd's vis function.  */
  76. static void
  77. strvis (dst, src, ignored)
  78.      char *dst, *src;
  79.      int ignored;
  80. {
  81.   int ch;
  82.   while (*src)
  83.     switch (ch = *src++)
  84.       {
  85.       case '\b': *dst++ = '\\'; *dst++ = 'b'; break;
  86.       case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
  87.       case '\t': *dst++ = '\\'; *dst++ = 't'; break;
  88.       case '\a': *dst++ = '\\'; *dst++ = 'a'; break;
  89.       case '\f': *dst++ = '\\'; *dst++ = 'f'; break;
  90.       case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
  91.       default:
  92.     if (isgraph (ch))
  93.       *dst++ = ch;
  94.     else
  95.       dst += sprintf (dst, "\\%03o", ch);
  96.       }
  97.   *dst = 0;
  98. }
  99. #endif /* !HAVE_VIS_H */
  100.  
  101. /*
  102.  * See if the user is accepting messages. If so, announce that 
  103.  * a talk is requested.
  104.  */
  105. announce(request, remote_machine)
  106.     CTL_MSG *request;
  107.     char *remote_machine;
  108. {
  109.     char full_tty[32];
  110.     FILE *tf;
  111.     struct stat stbuf;
  112.  
  113.     (void)snprintf(full_tty, sizeof(full_tty),
  114.         "%s%s", _PATH_DEV, request->r_tty);
  115.     if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
  116.         return (PERMISSION_DENIED);
  117.     return (print_mesg(request->r_tty, tf, request, remote_machine));
  118. }
  119.  
  120. #define max(a,b) ( (a) > (b) ? (a) : (b) )
  121. #define N_LINES 5
  122. #define N_CHARS 256
  123.  
  124. /*
  125.  * Build a block of characters containing the message. 
  126.  * It is sent blank filled and in a single block to
  127.  * try to keep the message in one piece if the recipient
  128.  * in in vi at the time
  129.  */
  130. print_mesg(tty, tf, request, remote_machine)
  131.     char *tty;
  132.     FILE *tf;
  133.     CTL_MSG *request;
  134.     char *remote_machine;
  135. {
  136.     struct timeval clock;
  137.     struct timezone zone;
  138.     struct tm *localtime();
  139.     struct tm *localclock;
  140.     struct iovec iovec;
  141.     char line_buf[N_LINES][N_CHARS];
  142.     int sizes[N_LINES];
  143.     char big_buf[N_LINES*N_CHARS];
  144.     char *bptr, *lptr, *vis_user;
  145.     int i, j, max_size;
  146.  
  147.     i = 0;
  148.     max_size = 0;
  149.     gettimeofday(&clock, &zone);
  150.     localclock = localtime( &clock.tv_sec );
  151.     (void)sprintf(line_buf[i], " ");
  152.     sizes[i] = strlen(line_buf[i]);
  153.     max_size = max(max_size, sizes[i]);
  154.     i++;
  155.     (void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
  156.     hostname, localclock->tm_hour , localclock->tm_min );
  157.     sizes[i] = strlen(line_buf[i]);
  158.     max_size = max(max_size, sizes[i]);
  159.     i++;
  160.     vis_user = (char *) malloc(strlen(request->l_name) * 4 + 1);
  161.     strvis(vis_user, request->l_name, VIS_CSTYLE);
  162.     (void)sprintf(line_buf[i], "talk: connection requested by %s@%s",
  163.         vis_user, remote_machine);
  164.     sizes[i] = strlen(line_buf[i]);
  165.     max_size = max(max_size, sizes[i]);
  166.     i++;
  167.     (void)sprintf(line_buf[i], "talk: respond with:  talk %s@%s",
  168.         vis_user, remote_machine);
  169.     sizes[i] = strlen(line_buf[i]);
  170.     max_size = max(max_size, sizes[i]);
  171.     i++;
  172.     (void)sprintf(line_buf[i], " ");
  173.     sizes[i] = strlen(line_buf[i]);
  174.     max_size = max(max_size, sizes[i]);
  175.     i++;
  176.     bptr = big_buf;
  177.     *bptr++ = ''; /* send something to wake them up */
  178.     *bptr++ = '\r';    /* add a \r in case of raw mode */
  179.     *bptr++ = '\n';
  180.     for (i = 0; i < N_LINES; i++) {
  181.         /* copy the line into the big buffer */
  182.         lptr = line_buf[i];
  183.         while (*lptr != '\0')
  184.             *(bptr++) = *(lptr++);
  185.         /* pad out the rest of the lines with blanks */
  186.         for (j = sizes[i]; j < max_size + 2; j++)
  187.             *(bptr++) = ' ';
  188.         *(bptr++) = '\r';    /* add a \r in case of raw mode */
  189.         *(bptr++) = '\n';
  190.     }
  191.     *bptr = '\0';
  192.     iovec.iov_base = big_buf;
  193.     iovec.iov_len = bptr - big_buf;
  194.     /*
  195.      * we choose a timeout of RING_WAIT-5 seconds so that we don't
  196.      * stack up processes trying to write messages to a tty
  197.      * that is permanently blocked.
  198.      */
  199.     if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
  200.         return (FAILED);
  201.  
  202.     return (SUCCESS);
  203. }
  204.